package edu.tec.mty.test.gui;

import edu.tec.mty.test.gui.canvas.*;
import edu.tec.mty.core.modules.*;
import edu.tec.mty.core.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Interface extends JFrame implements ActionListener, Runnable {

	private Menus menus;
	private InfoBoxes info;
	private JTextArea log;
	private static RC2DCanvas canvas;
	private Controller controller;
	private boolean isAlive;
	private UFrame uFrame;
	private ImageIcon icon;
	private Game game;
	private Self player;
	
	public Interface() {
		this.isAlive = false;
		this.uFrame = new UFrame();
		this.controller = new Controller();
		this.controller.setTeamName("Fawkes");
		//Menus
		this.menus = new Menus();
		this.menus.addMenu("File");
		this.menus.addMenu("Monitor");
		this.menus.addMenu("Team");
		this.menus.addMenu("Player");
		this.menus.addMenu("Help");
		try {
			this.menus.addItem("File", "Exit", this);
			this.menus.addItem("Monitor", "Show this player", this);
			this.menus.addItem("Monitor", "Show other players", this);
			this.menus.addItem("Monitor", "Show ball", this);
			this.menus.addItem("Monitor", "Show path", this);
			this.menus.addItem("Monitor", "Clear", this);
			this.menus.addItem("Team", "Set team name", this);
			this.menus.addItem("Player", "Connect", this);
			this.menus.addItem("Player", "Disconnect", this);
			this.menus.addItem("Help", "About", this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setJMenuBar(this.menus.getMenuBar());
		//Info
		this.info = new InfoBoxes();
		this.info.addInfoBox("Game");
		this.info.addInfoBox("Position");
		this.info.addInfoBox("Body");
		this.info.addInfoBox("Message");
		try {
			this.info.addLabel("Game", "Play mode");
			this.info.addLabel("Game", "Step");
			this.info.addLabel("Game", "Cycle");
			this.info.addLabel("Position", "X");
			this.info.addLabel("Position", "Y");
			this.info.addLabel("Position", "Direction");
			this.info.addLabel("Body", "Neck angle");
			this.info.addLabel("Body", "Speed");
			this.info.addLabel("Body", "Stamina");
			this.info.addLabel("Message", "From");
			this.info.addLabel("Message", "Time");
			this.info.addLabel("Message", "Message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		//Field
		this.canvas = new RC2DCanvas();
		JPanel jplMaster = new JPanel(new BorderLayout());
		JPanel upPanel = new JPanel(new BorderLayout());
		this.log = new JTextArea(5, 25);
		this.log.setFont(new Font("Arial", 0, 11));
		this.log.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(this.log);
		upPanel.add(this.canvas, BorderLayout.WEST);
		upPanel.add(scrollPane, BorderLayout.EAST);
		jplMaster.add(upPanel, BorderLayout.NORTH);
		jplMaster.add(this.info.getPanel(), BorderLayout.SOUTH);
		this.getContentPane().add(jplMaster, BorderLayout.CENTER);
		this.icon = new ImageIcon(Interface.class.getResource("res/article-edit.png"));
		try {
			this.info.setValue("Game", "Step", "0");
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.log.append("> disconnected\n> ");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("File.Exit")) {
			System.exit(0);
		}
		if (e.getActionCommand().equals("Monitor.Show other players")) {
			this.canvas.toggleDrawPlayers();
		}
		if (e.getActionCommand().equals("Team.Set team name")) {
			String teamName = "";
			boolean cancel = false;
			while(!cancel) {
				Object object = JOptionPane.showInputDialog(this, "Enter the team name:", "Set team name", JOptionPane.PLAIN_MESSAGE, this.icon, null, "Fawkes");
				if (object == null) {
					teamName = "";
					cancel = true;
				} else {
					teamName = (String)object;
					if (!teamName.equals("")) {
						this.controller.setTeamName(teamName);
						cancel = true;
						this.log.append("Team name changed to \"" + teamName + "\"\n> ");
					} else {
						this.log.append("Invalid team name\n> ");
					}
				}
			}
		}
		if (e.getActionCommand().equals("Player.Connect")) {
			String hostname = "";
			boolean cancel = false;
			while(!cancel) {
				Object object = JOptionPane.showInputDialog(this, "Enter the hostname:", "Connect to", JOptionPane.PLAIN_MESSAGE, this.icon, null, "localhost");
				if (object == null) {
					hostname = "";
					cancel = true;
				} else {
					hostname = (String)object;
					if (!hostname.equals("")) {
						try {
							this.controller.setTypeOfAgent('p');
							this.controller.connect(hostname);
							try {
								this.game = this.controller.getReferenceToGame();
								this.player = this.controller.getReferenceToPlayer();
								this.controller.getReferenceToCommands().move(-10.0, -10.0);
								this.isAlive = true;
								this.canvas.setController(this.controller);
								this.canvas.toggleDrawPlayer();
								this.canvas.toggleDrawBall();
								(new Thread(this)).start();
							} catch (Exception ex) {
								this.log.append("Error in modules retrieval\n> ");
							}
							cancel = true;
							this.log.append("Connected to " + hostname + "\n> ");
						} catch (Exception ex) {
							ex.printStackTrace();
							this.log.append("Host not found\n> ");
						}
					}
				}
			}
		}
		if (e.getActionCommand().equals("Player.Disconnect")) {
			this.isAlive = false;
			this.controller.disconnect();
			this.log.append("disconnected\n> ");
		}
		if (e.getActionCommand().equals("Help.About")) {
			String message = "Fawkes 1.0\n nt-labs 2012";
			JOptionPane.showMessageDialog(this, message,"About",JOptionPane.PLAIN_MESSAGE);
			return;
		}
	}
	
	@Override
	public void run() {
		while(this.isAlive) {
			//int step = this.controller.getStep();
			long step = this.game.getStep();
			try {
				this.info.setValue("Game","Step",Long.toString(step));
			} catch (Exception e) {
				
			}
			this.canvas.repaint();
		}
	}
	
}
